﻿//{imghref:图片文件夹所在路径,waitImg:等待图片名,bgImg:背景图片名}

(function (jqObj) {

    //1.定义一个 局部函数变量
    function NotifyBox() {
        var secondConst = 2000;//系统默认显示时间
        var secondWait = 2000;//显示时间
        var timer;//计时器
        var notifyBox;
        var text;
        var title;
        var callBackFunc;
        function hideNotifyJs() {
            //$('#notifyBox').remove();
            text.innerHTML = '';
            notifyBox.style.display = 'none';
            if (callBackFunc != undefined) {
                callBackFunc();
                callBackFunc = undefined;
            }
        }

        function startNotifyTimer(functionName) {
            clearNotifyTimer();
            timer = window.setTimeout(functionName, secondWait);
        }

        function clearNotifyTimer() {
            if (timer != null && timer != undefined) { clearTimeout(timer); }
        }

        function createNotifyBox() {
            notifyBox = document.createElement("div");
            notifyBox.setAttribute("id", "notifyBox");
            notifyBox.style.cssText = "display: none;top: 0px;left:0px;position: fixed;width: 100%;z-index: 9999;";

            var icons = document.createElement("div");
            icons.setAttribute("class", "alert-icons");
            notifyBox.appendChild(icons);

            title = document.createElement('b');
            notifyBox.appendChild(title);

            text = document.createElement('p');
            notifyBox.appendChild(text);

            document.body.appendChild(notifyBox);
        }
        // 关闭box
        this.hideNotify = function () {
            hideNotifyJs();
        };
        this.showNotifyOk = function (msg, callbackFuncP,time) {
            this.showNotify('提示!', msg, 'success', callbackFuncP, true, time);
        }
        this.showNotifyErr = function (msg, callbackFuncP,time) {
            this.showNotify('警告!', msg, 'err', callbackFuncP, true, time);
        }
        this.showNotifyInfo = function (msg, callbackFuncP,time) {
            this.showNotify('提示!', msg, 'info', callbackFuncP, true, time);
        }
        // 显示box
        this.showNotify = function (titlep, msg, leve, call, hide, time) {
            // 写入消息
            title.innerHTML = titlep;
            text.innerHTML = msg;
            callBackFunc = call;
            if (leve === 'info') {
                notifyBox.setAttribute("class", "alert alert-prompt");
            } else if (leve === 'err') {
                notifyBox.setAttribute("class", "alert alert-error");
            } else {
                notifyBox.setAttribute("class", "alert alert-success");
            }
            if (hide === true) {
                if (time === undefined) {
                    secondWait = secondConst;
                } else {
                    secondWait = time;
                }
                startNotifyTimer(hideNotifyJs);
            }
            $('#notifyBox').click(function () { $.notifyBox.hideNotify() });
            // 显示通知
            notifyBox.style.display = "block";
        };
        createNotifyBox();
    }

    //2.在 dom树创建完毕后，为 jq对象 添加一个 msgBoxObj对象（消息框对象）
    //  因为 MsbBox方法中 创建了一个文本框，并 添加到 了 body 元素中，所以需要等dom树加载完毕后执行
    jqObj(document).ready(function () {
        jqObj.extend(jqObj, {
            notifyBox: new NotifyBox()
        });
    });
})($);